package com.tcm.user.service;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.lang.StringUtils;
import com.base.core.utils.R;
import com.tcm.user.constants.enums.LoginTypeEnum;
import com.tcm.user.factory.Signup;
import com.tcm.user.factory.UserFactory;
import com.tcm.user.model.query.LoginAuthQuery;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@DubboService(version = "1.0.0")
public class SignupServiceImpl implements SignupService {

    @Autowired
    UserFactory signupFactory;

    @Override
    public R signup(LoginAuthQuery loginAuthQuery) {
        try {
            String type = loginAuthQuery.getLoginType().toUpperCase();
            type = LoginTypeEnum.getValue(type);
            if(StringUtils.isEmpty(type)){
                return R.fail("登录注册方式不正确");
            }
            Signup signup = signupFactory.createInstance(type);
            if (ObjectUtil.isEmpty(signup)){
                return R.fail("注册失败");
            }
            R ret = signup.save(loginAuthQuery);
            Map<String, Long> user = new HashMap<>();
            user.put("userId", (Long) ret.getData());
            return R.success(user);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }
}
